package com.hbxy.favorite.service.impl;

import com.hbxy.favorite.dao.*;
import com.hbxy.favorite.domain.*;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.CollectTypeService;
import com.hbxy.favorite.utils.RegUtil;
import com.hbxy.favorite.vo.CollectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CollectTypeServiceImpl  implements CollectTypeService {
    @Autowired
    CollectTypeMapper collectTypeMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    FavoritesMapper favoritesMapper;
    @Autowired
    CommentMapper commentMapper;
    @Override
    public ResponseBo getCollectType() {
        CollectTypeExample example = new CollectTypeExample();
        List<CollectType> collectTypeList = collectTypeMapper.selectByExample(example);

        return ResponseBo.ok(collectTypeList);
    }
    /**
     * 根据id查询收藏
     * @param title
     * @return
     */
    @Override
    public List<CollectVo> getCollectByid(String title) {

        List<CollectVo> collectVoList = new ArrayList<>();
        int size =0;
        CollectExample example =new CollectExample();
        CollectExample.Criteria criteria =example.createCriteria();
        criteria.andTitleLike("%"+ title+"%");
        List<Collect> collectList = collectMapper.selectByExample(example);



        for (Collect collect:collectList) {
            if( !"PRIVATE".equals(collect.getType())&& !"1".equals(collect.getIsDelete())   ){
                CollectVo Vo =new CollectVo();

                CommentExample example1 =new CommentExample();
                CommentExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andCollectIdEqualTo(collect.getId());
                List<Comment> commentList =commentMapper.selectByExample(example1);
                 size=commentList.size();
                if(RegUtil.isNotNull(collect.getFavoritesId()))
                {
                    Favorites favorites =favoritesMapper.selectByPrimaryKey(collect.getFavoritesId());
                    Vo.setFavoriteName(favorites.getName());
                }
                User user=userMapper.selectByPrimaryKey(collect.getUserId());
                Vo.setId(collect.getId());
                Vo.setTitle(collect.getTitle());
                Vo.setCategory(collect.getCategory());
                Vo.setUrl(collect.getUrl());
                Vo.setUserName(user.getUserName());
                Vo.setLogoUrl(collect.getLogoUrl());
                Vo.setCommentList(commentList);
                Vo.setCommentSize(size);
                collectVoList.add(Vo);

            }
        }
        return collectVoList;
    }
}
